<?php

/**
 * 逻辑区：大转盘抽奖
 * @copyright (C)2011 Cenwor Inc.
 * @author fengwei
 * @package logic
 * @name lunboad.logic.php
 * @version 1.0
 */

class LotteryLogic{
	public function add($ary){
		if (is_array($ary)) {
			$single = logic('lottery')->getOne($ary['uid']);
			if ($single) {
				dbc()->SetTable(table('lottery'));
				return dbc()->Update($ary, 'uid = '.$ary['uid']);
			}else{
				dbc()->SetTable(table('lottery'));
				return dbc()->Insert($ary);
			}
			
		}else{
			return false;
		}
	}
	
	public function cj_status($uid=0){
		if($uid<=0){
			return 0;
		}else{
			$single = logic('lottery')->getOne($uid);
			if ($single['prizeid']) {
				return 2;
			}else{
				$a_date = date('Y-m-d',$single['time']);
				$b_date = date('Y-m-d');
				if($a_date==$b_date){
					return 3;
				}else{
					return 1;
				}
			}
		}
		
	}
	
	public function getOne($uid){
		dbc()->SetTable(table('lottery'));
		$result = dbc()->select('','uid='.$uid);
		//$result = dbc(DBCMax)->select('lottery')->where('uid='.$uid)->done();
		return $result;
	}
	
	public function getList(){
		$sql = 'SELECT * from  '.table('lottery').' WHERE prizeid>0 ORDER BY time desc';
		$prize_list = dbc(DBCMax)->query($sql)->done();
		return $prize_list;
	}
	
	public function getPrizeArr(){
		$first = 2;
		$second = 5;
		$third = 20;
		$prize_arr = array(
				'0' => array('id'=>0,'min'=>array(46,92,182,273,317),
						'max'=>array(87,132,222,312,357),'prize'=>'没中奖','v'=>9897),
				'1' => array('id'=>1,'min'=>137,'max'=>178,'prize'=>'一等奖','v'=>1),
				'2' => array('id'=>2,'min'=>227,'max'=>268,'prize'=>'二等奖','v'=>2),
				'3' => array('id'=>3,'min'=>1,'max'=>44,'prize'=>'三等奖','v'=>100)
		
		
		);
		$oneNum = $this->getprizeNum(1);
		if( $oneNum>= $first){
			$prize_arr[1]['v'] = 0;
		}
		
		$twoNum = $this->getprizeNum(2);
		if( $twoNum>= $second){
			$prize_arr[2]['v'] = 0;
		}
		$threeNum = $this->getprizeNum(3);
		if( $threeNum>= $third){
			$prize_arr[3]['v'] = 0;
		}
		
		return $prize_arr;
	}
	
	private function getPrizeNum($prizeid){
		$sql = 'SELECT count(id) as count from  '.table('lottery').' WHERE prizeid='.$prizeid.' ORDER BY time desc';
		$result = dbc(DBCMax)->query($sql)->limit(1)->done();
		return $result['count'];
	}
}
